﻿using System;
using System.Windows.Forms;

namespace CodePlex.DnD4eCampaignManager.ControlLibrary
{
    public partial class ModalNumberPicker : Form
    {
        public ModalNumberPicker()
        {
            InitializeComponent();
        }

        private void ModalNumberPicker_Load(object sender, EventArgs e)
        {
            NumberToPickValue.Select();
            NumberToPickValue.Select(0, NumberToPickValue.Value.ToString().Length);
        }

        private void NumberToPickValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ( e.KeyChar == '\r' )
            {
                SetButton_Click(sender, e);
            }
        }

        private void SetButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        public static decimal GetNumber(string title, string message, decimal minValue, decimal maxValue, decimal suggestedValue, int width)
        {
            ModalNumberPicker picker = new ModalNumberPicker();
            picker.Text = title;
            picker.NumberToPickName.Text = message;
            picker.NumberToPickValue.Minimum = minValue;
            picker.NumberToPickValue.Maximum = maxValue;
            picker.NumberToPickValue.Value = suggestedValue;
            picker.Width = width;
            picker.ShowDialog();
            return picker.NumberToPickValue.Value;
        }
    }
}